package com.ihisomp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ihisomp.base.Page;
import com.ihisomp.base.Result;
import com.ihisomp.dto.SysCityDto;
import com.ihisomp.entity.SysCity;
import com.ihisomp.mapper.SysCityMapper;
import com.ihisomp.service.ISysCityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ihisomp.service.ISysGetNoService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * <p>
 * 省市县信息 服务实现类
 * </p>
 *
 * @author zy
 * @since 2022-01-12
 */
@Service
public class SysCityServiceImpl extends ServiceImpl<SysCityMapper, SysCity> implements ISysCityService {

    @Autowired
    private ISysGetNoService sysGetNoService;

    /**
     * 新增数据
     *
     * @param sysCityDto
     * @return
     */
    @Override
    public Result add(SysCityDto sysCityDto) {
        if (sysCityDto == null || StringUtils.isBlank(sysCityDto.getCityCode())) {
            return Result.error("省市县编码不能为空");
        }
        if (StringUtils.isBlank(sysCityDto.getCityName())) {
            return Result.error("省市县名称不能为空");
        }
        if (getByCityCode(sysCityDto.getCityCode()) != null) {
            return Result.error("省市县已经存在！");
        }
        SysCity sysCity = new SysCity();
        BeanUtils.copyProperties(sysCityDto, sysCity);
        String cityKey = sysGetNoService.getSysNo("t_sys_city");
        sysCity.setCityKey(cityKey);
        if (baseMapper.insert(sysCity) > 0) {
            return Result.ok();
        }
        return Result.error("新增省市县失败！");
    }

    /**
     * 删除数据
     *
     * @param cityKey
     * @return
     */
    @Override
    public Result delete(String cityKey) {
        SysCity sysCity = baseMapper.selectById(cityKey);
        if (sysCity == null) {
            return Result.error("无查询省市县信息！");
        }
        if (baseMapper.deleteById(sysCity) > 0) {
            return Result.ok();
        }
        return Result.error("删除省市县失败！");
    }

    /**
     * 更新数据
     *
     * @param cityKey
     * @param sysCityDto
     * @return
     */
    @Override
    public Result update(String cityKey, SysCityDto sysCityDto) {
        SysCity sysCity = new SysCity();
        BeanUtils.copyProperties(sysCityDto, sysCity);
        if (sysCity.getCityKey() == null) {
            sysCity.setCityKey(cityKey);
        }
        if (baseMapper.updateById(sysCity) > 0) {
            return Result.ok();
        } else {
            return Result.error("更新省市县失败！");
        }
    }

    /**
     * 查询数据
     *
     * @param cityKey
     * @return
     */
    @Override
    public Result get(String cityKey) {
        SysCity sysCity = baseMapper.selectById(cityKey);
        if (sysCity == null) {
            return Result.error("无查询省市县信息！");
        }
        return Result.ok(sysCity);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @param sysCityDto
     * @return
     */
    @Override
    public Result search(long page, long size, SysCityDto sysCityDto) {
        //  查询条件
        QueryWrapper<SysCity> query = new QueryWrapper<>();
        if (sysCityDto != null) {
            if (StringUtils.isNotBlank(sysCityDto.getCityName())) {
                query.like("city_name", sysCityDto.getCityName());
            }
            if (sysCityDto.getCityLevel() != null) {
                query.eq("city_level", sysCityDto.getCityLevel());
            }
        }
        query.orderByAsc("city_code");
        // 分页查询
        IPage<SysCity> data = baseMapper.selectPage(new Page<>(page, size), query);
        return Result.ok(data);
    }

    /**
     * 省市县列表
     * @param sysCityDto
     * @return
     */
    @Override
    public Result selectDataList(SysCityDto sysCityDto) {
        List<SysCityDto> list = baseMapper.selectDataList(sysCityDto);
        SysCityDto dto = new SysCityDto();
        dto.setCityCode("");
        dto.setCityName("");
        list.add(0, dto);
        return Result.ok(list);
    }

    /**
     * 查询省市县是否存在
     *
     * @param cityCode
     * @return
     */
    private SysCity getByCityCode(String cityCode) {
        QueryWrapper<SysCity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("city_code", cityCode);
        return baseMapper.selectOne(queryWrapper);
    }

}
